package com.groupfour.attendancemanagementsystem.controller;


import com.groupfour.attendancemanagementsystem.entity.DO.ProjectGroupDO;
import com.groupfour.attendancemanagementsystem.entity.DO.ProjectGroupMembersDO;
import com.groupfour.attendancemanagementsystem.entity.DO.StudentDO;
import com.groupfour.attendancemanagementsystem.service.ProjectGroupService;
import com.groupfour.attendancemanagementsystem.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@CrossOrigin
@RestController
public class ProjectGroupController {

    private ProjectGroupService projectGroupService;
    @Autowired
    public ProjectGroupController(ProjectGroupService projectGroupService) {
        this.projectGroupService = projectGroupService;
    }

    /**
     * @author
     * @param projectGroupDO
     * @return
     * @describe
     * 添加项目组,只需要传项目组的名字
     */
    @PostMapping("/addProject")
    public Result<String> addProject(ProjectGroupDO projectGroupDO){
        int i = projectGroupService.addProject(projectGroupDO);
        if (i>0){
            return Result.ok("添加成功");
        }
        return Result.fail("添加失败");
    }

    /**
     * 项目项目组成员
     * @param groupCode
     * @param studentCode
     * @return
     */
    @PostMapping("/updateMember")
    public Result updateMember(String groupCode,String studentCode){
        int i = projectGroupService.updataMember(groupCode, studentCode);
        if (i>0){
            return Result.ok("修改成功");
        }
        return Result.fail("修改失败");
    }

    /**
     * 添加项目组成员
     * @param projectGroupMembersDO
     * @param request
     * @return
     */
    @PostMapping("/addMember")
    public Result addMember(ProjectGroupMembersDO projectGroupMembersDO, HttpServletRequest request){
        int i = projectGroupService.addMember(projectGroupMembersDO,request);
        if (i>0){
            return Result.ok("添加成功");
        }
        return Result.fail("添加失败");
    }

    /**
     * 修改项目组
     * @param projectGroupDO
     * @return
     */
    @PostMapping("/updateProject")
    public Result updateProject(ProjectGroupDO projectGroupDO){
        int i = projectGroupService.updateProject(projectGroupDO);
        if (i>0){
            return Result.ok("修改成功");
        }
        return Result.fail("修改失败");
    }

    /**
     * 删除项目组
     * @param groupId
     * @return
     */
    @GetMapping("/deleteProject")
    public Result<String> deleteProject(int groupId){
        int i = projectGroupService.deleteProject(groupId);
        if (i>0){
            return Result.ok("删除成功");
        }
        return Result.fail("删除失败");
    }

    /**
     * 删除项目组成员
     * @param studentCode
     * @return
     */
    @PostMapping("/deleteMember")
    public Result deleteMember(int studentCode){
        int i = projectGroupService.deleteMember(studentCode);
        if (i>0){
            return Result.ok("删除成功");
        }
        return Result.fail("删除失败");
    }

    /**
     * 通过是否结项查询项目组
     * @param isGraduation
     * @param request
     * @return
     */
    @GetMapping("/selectProject")
    public Result selectProject(int isGraduation, HttpServletRequest request){
        List<ProjectGroupDO> projectGroupDaos = projectGroupService.selectProject(isGraduation);
        request.setAttribute("projectGroupDaos",projectGroupDaos);
        return Result.ok(projectGroupDaos);
    }

    /**
     * 通过id查询项目组
     * @param groupId
     * @param request
     * @return
     */
    @GetMapping("/queryProject")
    public Result queryProject(String groupId,HttpServletRequest request){
        ProjectGroupDO projectGroupDO = projectGroupService.queryProject(groupId, request);
        if (projectGroupDO!=null){
            return Result.ok(projectGroupDO);
        }
        return Result.fail("查询失败");
    }

    /***
     * 按项目组名称搜索项目组
     * @param groupName
     * @return
     */
    @GetMapping("/queryByNameProject")
    public Result queryByNameProject(String groupName){
        ProjectGroupDO projectGroupDO = projectGroupService.queryByNameProject(groupName);
        if (projectGroupDO!=null){
            return Result.ok(projectGroupDO);
        }
        return Result.fail("查找失败");
    }

    /**
     * 整合了学生，老师，页面的条件搜索
     * @param groupName
     * @param departmentName
     * @param majorName
     * @param gradeName
     * @param classesName
     * @param studentName
     * @param studentCode
     * @param studentId
     * @param teacherId
     * @param groupId
     * @return
     */
    @PostMapping("/selectMember")
    public Result selectMember(String groupName,String departmentName,String majorName,String gradeName,String classesName,String studentName,String studentCode,int studentId,int teacherId,int groupId){
        List<StudentDO> studentDOS = projectGroupService.selectMember(groupName,departmentName,majorName,gradeName,classesName,studentName,studentCode,studentId,teacherId,groupId);
        if (studentDOS!=null){
            return Result.ok("查询成功");
        }
        return Result.fail("查询失败");
    }

    /**
     * 添加成绩和评论
     * @param groupComment
     * @param groupSyntnesisScore
     * @param groupId
     * @return
     */
    @PostMapping("/addFraction")
    public Result addFraction(String groupComment,double groupSyntnesisScore,int groupId){
        int i = projectGroupService.addFraction(groupComment,groupSyntnesisScore,groupId);
        if (i>0){
            return Result.ok("添加成功");
        }
        return Result.ok("添加失败");
    }

    /**
     * 修改项目角色
     * @param isGroupLeader
     * @param groupCode
     * @return
     */
    @GetMapping("/updateIsGroupLeader")
    public Result updateIsGroupleader(int isGroupLeader,String groupCode){
        int i = projectGroupService.updateIsGroupleader(isGroupLeader, groupCode);
        if (i>0){
            return Result.ok("修改成功");
        }
        return Result.fail("修改失败");
    }

    /**
     * 查询所有的项目组
     * @return
     */
    @GetMapping("/selectProjectAll")
    public Result selectProjectAll(){
        List<ProjectGroupDO> projectGroupDOS = projectGroupService.selectProjectAll();
        if (projectGroupDOS!=null){
            return Result.ok(projectGroupDOS);
        }
        return Result.fail("查看失败");
    }
}
